package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.UserInfoEntity;
import com.cheng.data.entity.UserRoleEntity;
import com.cheng.data.mapper.UserInfoMapper;
import com.cheng.data.vo.UserInfoVO;
import com.cheng.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/04 21:12
 * @version: 1.0
 * @description: userinfoEntity 处理类
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    private final static Logger log = LoggerFactory.getLogger(UserInfoServiceImpl.class);

    @Autowired
    UserInfoMapper userInfoMapper;

    /**
     * @author: ChengJW
     * @params: [userName]
     * @return: com.cheng.data.vo.UserInfoVO
     * @date: 2021/11/7
     * @description: 根据用户名称查询用户信息
     */
    @Override
    public UserInfoVO queryUserInfoByUserName(UserInfoEntity userInfoEntity) throws CommonException {

        if (null == userInfoEntity){
            log.error("userInfoEntity is null");
            throw new CommonException("CJW202111010021","UserServiceImpl","queryUserInfoByUserName");
        }
        QueryWrapper<UserInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(userInfoEntity.getUrNm()),"ur_Nm",userInfoEntity.getUrNm());
        UserInfoEntity entity = userInfoMapper.selectOne(wrapper);
        UserInfoVO userInfoVO = new UserInfoVO();
        if (entity != null){
            BeanUtils.copyProperties(entity,userInfoVO);
        }
        return userInfoVO;
    }

    /**
     * @author: ChengJW
     * @params: [userInfoEntity]
     * @return: java.util.List<com.cheng.data.vo.UserInfoVO>
     * @date: 2021/11/14
     * @description: 查询用户列表信息
     */
    @Override
    public List<UserInfoVO> queryUserList(UserInfoEntity userInfoEntity) throws CommonException {
        return userInfoMapper.queryUserList(userInfoEntity);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: int
    * @date: 2021/11/14
    * @description: 新增用户信息
    */
    @Override
    public int insertUserInfo(UserInfoEntity entity) throws CommonException {
        return userInfoMapper.insert(entity);
    }

    /**
     * @author: ChengJW
     * @params: [urNm]
     * @return: java.util.List<com.cheng.data.vo.UserInfoVO>
     * @date: 2021/11/14
     * @description: 根据用户名称，查询用户信息
     */
    @Override
    public List<UserInfoEntity> queryUserListByNm(String urNm) throws CommonException {

        QueryWrapper<UserInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(urNm),"ur_Nm",urNm);
        return userInfoMapper.selectList(wrapper);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: int
    * @date: 2021/11/14
    * @description: 修改用户信息
    */
    @Override
    public int updateUserInfo(UserInfoEntity entity) throws CommonException {
        return userInfoMapper.updateById(entity);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: com.cheng.data.entity.UserInfoEntity
    * @date: 2021/11/14
    * @description: 根据主键查询用户信息
    */
    @Override
    public UserInfoEntity queryUserInfo(String urId) throws CommonException {
        return userInfoMapper.selectById(urId);
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: int
    * @date: 2021/11/14
    * @description: 删除用户信息
    */
    @Override
    public int deleteUserInfo(UserInfoEntity entity) throws CommonException {
        return userInfoMapper.deleteById(entity.getUrId());
    }

    /**
     * @author: ChengJW
     * @params: [urNm]
     * @return: com.cheng.data.vo.UserInfoVO
     * @date: 2021/11/15
     * @description: 根据用户名称，查询用户所有的信息，包括角色、权限信息
     */
    @Override
    public UserInfoVO queryUserBaseInfoByNm(String urNm) throws CommonException {
        UserInfoEntity entity = new UserInfoEntity();
        entity.setUrNm(urNm);
        return userInfoMapper.queryBaseUserInfo(entity);
    }
}
